import tools from "@/util/tools.js"
import c from '@/common/config'

import {
	userStore
} from "@/store"

/**
 * 请求拦截
 * @param {Object} http
 */
export const requestInterceptors = (vm) => {
	let user = userStore();
	uni.$rui.http.interceptors.request.use((config) => {
			// 可使用async await 做异步操作
			// 初始化请求拦截器时，会执行此方法，此时data为undefined，赋予默认{}
			config.data = config.data || {}
			config.params = {
				...config.params,
				...c.baseApi
			}
			config.params.timestamp = Date.now()
			config.params.sign = tools.createSign(config.params, config.data, c.baseKey)

			config.header.token = user.getToken

			config.header.accept = 'application/json'
			return config
		}, (config) => // 可使用async await 做异步操作
		Promise.reject(config))
}